package com.teaching.management.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 确保上传目录存在
        createUploadDirectories();

        // 配置静态资源映射
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:uploads/");
    }

    private void createUploadDirectories() {
        // 创建头像上传目录
        String[] uploadDirs = {
                "uploads/avatars/teachers/",
                "uploads/avatars/students/",
                "uploads/avatars/admins/"
        };

        for (String dir : uploadDirs) {
            File uploadDir = new File(dir);
            if (!uploadDir.exists()) {
                uploadDir.mkdirs();
            }
        }
    }
}